package com.central.product.service.serviceImp

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.util.RandomUtil
import cn.hutool.core.util.StrUtil
import com.alibaba.fastjson.JSONObject
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.central.common.entity.DrawProduct
import com.central.common.entity.DrawProductGroup
import com.central.common.enum.ResultCode
import com.central.common.model.CommonResult
import com.central.common.model.PageVo
import com.central.common.model.failed
import com.central.common.model.success
import com.central.common.service.impl.SuperServiceImpl
import com.central.product.mapper.DrawProductGroupMapper
import com.central.product.pojo.drawProductGroup.*
import com.central.product.service.DrawCategoryService
import com.central.product.service.DrawProductGroupService
import com.central.product.service.DrawProductService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

/**
 * @author  fly
 * @date  2021/5/26 14:53
 * @version 1.0
 */
@Service
open class DrawProductGroupServiceImp : SuperServiceImpl<DrawProductGroupMapper, DrawProductGroup>(), DrawProductGroupService {


    @Autowired
    private lateinit var drawProductService: DrawProductService

    @Autowired
    private lateinit var drawCategoryService: DrawCategoryService


    override fun saveDrawProductGroup(productDto: DrawProductDto): CommonResult<Any> {

        val drawProductGroup = DrawProductGroup()
       return save(drawProductGroup.apply { BeanUtil.copyProperties(productDto, this) })
            .takeIf { it }
            ?.run {
                JSONObject.parseArray(productDto.drawProductInfos, DrawProductVo::class.java)
                    .takeIf { it.isNotEmpty() }
                    ?.run {
                        forEach { drawProductVo ->
                            val drawProduct = DrawProduct()
                            drawProductService.save(drawProduct.apply {
                                BeanUtil.copyProperties(drawProductVo, this)
                                drawProductGroupId = drawProductGroup.id
                            })
                        }
                    }
                success("保存成功")
            } ?: failed("保存抽奖组失败")
    }

    override fun removeDrawProductGroup(ids: String): CommonResult<Boolean> = success(
        removeByIds(ids.split(","))
    , "批量移除成功")

    override fun updateDrawProductGroup(productDto: DrawProductDto): CommonResult<Any> {

        getById(productDto.id)?.run {
            updateById(this.apply { BeanUtil.copyProperties(productDto, this) })
                .takeIf { it }
                ?.run {
                    JSONObject.parseArray(productDto.drawProductInfos, DrawProductVo::class.java)
                        .takeIf { it.isNotEmpty() }
                        ?.run {
                            drawProductService.remove(KtQueryWrapper(DrawProduct::class.java)
                                .eq(DrawProduct::drawProductGroupId, productDto.id))
                            forEach {
                                drawProductService.save(DrawProduct().apply {
                                    BeanUtil.copyProperties(it, this)
                                    drawProductGroupId = productDto.id
                                })
                            }
                        }
                }
        }
        return success("修改成功")
    }

    override fun getDrawProductList(productGroupSearchDto: DrawProductGroupSearchDto, pageVo: PageVo): CommonResult<Page<DrawProductListVo>> =
        page(Page(pageVo.pageNumber!!, pageVo.pageSize!!), KtQueryWrapper(DrawProductGroup::class.java)
            .eq(productGroupSearchDto.drawCategoryId != null, DrawProductGroup::drawCategoryId, productGroupSearchDto.drawCategoryId)
            .like(StrUtil.isNotBlank(productGroupSearchDto.drawProductGroupName), DrawProductGroup::drawProductGroupName, productGroupSearchDto.drawProductGroupName)
            .orderByDesc(DrawProductGroup::createTime))
            .run {

                records.takeIf { it.isNotEmpty() }
                    ?.let {
                        success(
                            Page<DrawProductListVo>(current, size, total, isSearchCount).apply {
                                records = it.mapNotNull { drawProductGroup ->
                                    DrawProductListVo().apply {
                                        BeanUtil.copyProperties(drawProductGroup, this)
                                        drawProductGroupId = drawProductGroup.id
                                        drawProductPics = drawProductService.getDrawProductVoByProductGroupId(drawProductGroup.id)
                                            ?.mapNotNull { it.drawProductPic }?.joinToString(",")
                                    }
                                }
                            }
                            , "获取抽奖组商品成功")
                    } ?: failed(ResultCode.NO_CONTENT)
            }
    override fun getDrawProductGroupDetail(id: Long): CommonResult<DrawProductDetailVo> =
        getById(id)?.let {
            success(
                DrawProductDetailVo().apply {
                    BeanUtil.copyProperties(it, this)
                    drawCategoryName = drawCategoryService.getById(it.drawCategoryId)?.title
                    drawProductVos = drawProductService.getDrawProductVoByProductGroupId(id)
                }
                , "获取详情成功")
        } ?: failed(ResultCode.NO_CONTENT)

    override fun getDrawResult(drawProductGroupId: Long): CommonResult<DrawProductVo> =
        getById(drawProductGroupId)?.run {
            drawProductService.getDrawProductVoByProductGroupId(drawProductGroupId)?.run {
                success(RandomUtil.randomEle(this))
            }
        } ?: failed(ResultCode.NO_CONTENT)

}
